#include "DirScanner.h"
#include <sys/types.h>
#include <dirent.h>
#include <string.h>

void DirScanner::operator()(const string &dirname)
{
    traverse(dirname);
}

void DirScanner::traverse(const string & dirname)
{
    DIR* dir;
    struct dirent* p;
    const char* path = dirname.c_str();
    if((dir = opendir(path)) == NULL)
    {
        perror("opendir");
        exit(1);
    }
    while((p = readdir(dir)) != NULL)
    {
        if(strcmp(p->d_name, ".") == 0 || strcmp(p->d_name, "..") == 0)
            continue;
        else if(p->d_type == 8)//此时是普通文件
            _files.push_back(dirname + "/" + p->d_name);
        else if(p->d_type == 10)//此时是链接文件
            continue;
        else if(p->d_type == 4)
        {//此时是子目录
            const string tmp = dirname + "/" + p->d_name;
            DirScanner::traverse(tmp);
        }
    }
    closedir(dir);
}


